import { Component, Input, OnInit } from '@angular/core';
import { NzTableQueryParams } from 'ng-zorro-antd/table';
import { BehaviorSubject } from 'rxjs';
import { Consts } from 'src/app/consts/consts';
import { IntIdParam } from 'src/app/models/param/int-id.param';
import { GroupDto } from 'src/app/models/sys/group.dto';
import { ApiService } from 'src/app/services/api.service';

@Component({
  selector: 'app-role-groups',
  templateUrl: './role-groups.component.html',
  styleUrls: ['./role-groups.component.less']
})
export class RoleGroupsComponent implements OnInit {
  @Input() roleIdSubject = new BehaviorSubject<number>(0);
  roleId?: number;

  groups: GroupDto[] = [];
  pageIndex = 1;
  pageSize = 10;
  total = 0;

  constructor(private _api: ApiService) {}

  ngOnInit(): void {
    this.roleIdSubject.subscribe((value) => {
      this.roleId = value;
    });

    this.onSearch();
  }

  onSearch() {
    const param = new IntIdParam();
    param.id = this.roleId;
    param.pageNum = this.pageIndex;
    param.pageSize = this.pageSize;

    this._api.getGroupsByRole(param).subscribe((resp) => {
      if (resp.code == Consts.RESP_OK && resp.data) {
        this.groups = resp.data;
        this.total = resp.total;
      }
    });
  }

  onPageParamChange(params: NzTableQueryParams) {
    this.pageIndex = params.pageIndex;
    this.pageSize = params.pageSize;
    this.onSearch();
  }
}
